package Classes.GameDatas// ActionScript file
{
	import Classes.CustomEvents.ViewDataChangedEvent;
	import Classes.GameErrors.GameError;
	import Classes.GameUtils.GU_Sound;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_HavenWall;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapNPCs.MN_Zombie;
	import Classes.GameViews.ViewPanels.VP_Main;
	import Classes.GameViews.ViewPanels.VP_TextWinPanel;
	import Classes.Interfaces.GV_hot;
	import Classes.Managers.M_Communication;
	import Classes.staticinfo.Language;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	/**
	 *避难所围墙数据类 
	 * @author 葛增文
	 * 
	 */	
	public class GD_HavenWall extends GameData implements GV_hot
	{
		//private var _map:GD_Map;/*所属地图*/
		private var _wallSerial:int;/*数组序列值*/
		//private var _positionPoint:Point;/*围墙位置*/
		private var _locationType:int;/*1:左上,2:上,3:右上,4:右,5:右下,6:下,7:左下,8:左*/
		private var _atx:int;/*围墙所在X坐标*/
		private var _aty:int;/*围墙所在Y坐标*/
		private var _type:int;/*围墙类型*/
		private var _dur:int;/*耐久度*/
		private var _view:MT_HavenWall;/*避难所围墙显示对象*/
		
		public function GD_HavenWall():void
		{
			
		}
		
		/**
		 *创建一个避难所围墙对象 
		 * @param creatObj 创建数据对象
		 * @return 避难所围墙对象
		 * 
		 */		
		public static function creat(creatObj:Object):GD_HavenWall
		{
			var returnWall:GD_HavenWall=new GD_HavenWall();
			//returnWall._map=map;
			returnWall._wallSerial=creatObj.wallSerial;
			returnWall._type=creatObj.type;
			returnWall._dur=creatObj.dur;
			//returnWall.atx=creatObj.atx;
			//returnWall.aty=creatObj.aty;
			//returnWall.dur=creatObj.dur;
			return returnWall;
		}
		
		/**
		 *设定位置数据 
		 * @param atx
		 * @param aty
		 * @param locationType
		 * 
		 */		
		public function setPositionData(atx:int,aty:int,locationType:int):void
		{
			this._atx=atx;
			this._aty=aty;
			this._locationType=locationType;
		}
		
		/**
		 *释放 
		 * 
		 */		
		override public function release():void
		{
			//			super.release();
			if(this._view)
			{
				this._view.removeListener();
				this._view=null;
			}
		}
		
		/*getter & setter*/
		public function get positionPoint():Point
		{
			return new Point(this.atx,this.aty);
		}
		
		public function get type():int
		{
			return _type;
		}
		
		public function set type(value:int):void
		{
			if(_type!=value)
			{
				_type=value;
				this.view.dispatchEvent(new ViewDataChangedEvent(ViewDataChangedEvent.DATACHANGED,"type"));
			}
		}
		
		public function get dur():int
		{
			return _dur;
		}
		
		public function set dur(value:int):void
		{
			if(_dur!=value)
			{
				_dur=value;
				this.view.dispatchEvent(new ViewDataChangedEvent(ViewDataChangedEvent.DATACHANGED,"dur"));
				this.touchObserve("life");
				
//				if(_dur<2)
					inbreakCheck();					//入侵检查
			}
		}
		
		/**
		 * 围墙序列号 即围墙ID 
		 * @return 
		 * 
		 */		
		public function get wallSerial():int
		{
			return _wallSerial
		}
		
		public function get view():MT_HavenWall
		{
			if(!_view)_view=MT_HavenWall.creat(this);
			return _view;
		}
		
		public function get atx():int
		{
			return _atx;
		}
		
		public function set atx(value:int):void
		{
			_atx=value;
			/*if(_atx!=value)
			{
			_atx = value;
			this.view.dispatchEvent(new ViewDataChangedEvent(ViewDataChangedEvent.DATACHANGED,"positionPoint"));				
			}*/
		}
		
		public function get aty():int
		{
			return _aty;
		}
		
		public function set aty(value:int):void
		{
			_aty=value;
			/*if(_aty!=value)
			{
			_aty = value;
			this.view.dispatchEvent(new ViewDataChangedEvent(ViewDataChangedEvent.DATACHANGED,"positionPoint"));				
			}*/
		}
		
		public function get locationType():int
		{
			return _locationType;
		}
		
		//****************热感的属性******************
		public function get currentlife():int
		{
			return this.dur
		}
		public function get lifeMax():int
		{
			return (GV_Map.instance.mapData.havenLv+2);
		}
		public function get sxTxt():String
		{
			return "";
		}
		public function get sxTxtCount():uint
		{
			return 0;
		}
		
		public function get gvName():String
		{
			var name:String="";
			switch(this._type)
			{
				case 0:
					name="矮木围墙";
					break;
				case 1:
					name="木栅栏";
					break;
				case 2:
					name="细铁围墙";
					break;
				case 3:
					name="铁刺围墙";
					break;
				case 4:
					name="石头围墙";
					break;
			}
			return name
		}
		
		public function addObserverFuncs(observerType:String,obderverObj:*):void
		{
			this.addObserver(observerType,obderverObj);
		}
		
		public function removeOberverFuns(observerType:String,obderverObj:*):void
		{
			this.removeObserver(observerType,obderverObj);
		}
		
		//**********************************
		
		
		//****************************僵尸入侵模块*********************
		public static var  inbreakAry:Array=new Array;/*入侵的僵尸*/
		/**
		 *是否是入侵的状态 
		 */
		public static var isInbreak:Boolean=false;
		/**
		 *设置围墙是否可见 
		 */
		public static function setWallsVisible():void
		{
			var wallData:GD_HavenWall;
			var bd1:BitmapData;//= new BitmapData(tsp.width,tsp.height);
			var bd2:BitmapData;
			var bm1:Bitmap;
			var bm2:Bitmap;
			for(var i:uint=0; i<GV_Map.instance.mapData.wallsAry.length; i++)
			{
				wallData	=	GV_Map.instance.mapData.wallsAry[i] as GD_HavenWall;
				var tsp:MT_HavenWall	=	wallData.view;
				
				
				
				if(tsp.mapGrid.building!=null){
					var isVisible:Boolean=false;
					
					for( var fi:uint=0; fi<GV_Map.instance.floorTargetsAry.length; fi++)
						if( tsp.mapGrid.building	== GV_Map.instance.floorTargetsAry[fi]){
							isVisible=true;
							break;
						}
					tsp.visible = isVisible;
				}
				else{
					tsp.visible = true;
				}
			}
			inbreakCheck();
		}
		
		/**
		 *设置入侵状态 
		 */
		public static function setIsInbreak(value:Boolean):void
		{
			if(isInbreak!=value){
				if(value){
					(VP_Main.instance.interfaceUI["alertor"] as Sprite).buttonMode = true;
					(VP_Main.instance.interfaceUI["alertor"] as Sprite).addEventListener(MouseEvent.CLICK,clickAlertor);
					GU_Sound.instance.playInfinitySound("System_Alert");
				}
				else{
					(VP_Main.instance.interfaceUI["alertor"] as Sprite).removeEventListener(MouseEvent.CLICK,clickAlertor);
					GU_Sound.instance.closeInfinitySound();
				}
			}
			
			isInbreak	=	value;	
			VP_Main.instance.interfaceUI["alertor"].visible	=	isInbreak;
		}
		public static var badWallNum:uint=0;
		public static var jsNum:uint=0;
		public static function clickAlertor(e:Event):void
		{
			VP_TextWinPanel.instance.openTextWindon(Language.alertorHitTitle,(badWallNum==0?"":Language.alertorHitConfirm1+badWallNum+"\n" )+(jsNum==0?"":Language.alertorHitConfirm2+jsNum) );
		}
		
		private static var isHome:Boolean=true;
		public static function setIsHome(value:Boolean):void
		{
			isHome	=	value;
			//			inbreakCheck();
		}
		/**
		 *僵尸入侵检查 
		 */
		public static function inbreakCheck():Boolean
		{
			if(!isHome || null==GV_Map.instance.mapData){setIsInbreak(false);return false;}
			badWallNum = 0;
			jsNum		= 0;
			var havaBadWall:Boolean=false;
			var havaJS:Boolean=false;
			var wallData:GD_HavenWall;
			for(var i:uint=0; i<GV_Map.instance.mapData.wallsAry.length; i++)
			{
				wallData	=	GV_Map.instance.mapData.wallsAry[i] as GD_HavenWall;
				var tsp:MT_HavenWall	=	wallData.view;
				if(wallData.currentlife<1 && wallData.view.visible==true){
					wallData.view.setState(10002000,true);
					havaBadWall	=	 true;
					badWallNum++;
				}
				else{
					wallData.view.setState(10002000,false);
				}
				
				//新手指导判断
				if(GD_Player.instance.playPowerObj[10000217] && GD_Player.instance.missions[11000001]&& GD_Player.instance.missions[11000001].missionState!="over" && wallData.currentlife<3 && wallData.view.visible==true  && (wallData._wallSerial==1 || wallData._wallSerial==20 || wallData._wallSerial==5))
				{
					wallData.view.setState(10002000,true);
					havaBadWall	=	 false;
					badWallNum++;
				}
				
				//				else
				//					wallData.view.setState(10002000,false);
			}
			
//			var layerMapTargetsChildren:Number=GV_Map.instance.layerMapTargets.numChildren;
			var layerMapTargetsChildren:Number=GV_Map.instance.mapTargetsAry.length;
			for(i=0;i<layerMapTargetsChildren;i++)
			{
				var js:MN_Zombie = GV_Map.instance.mapTargetsAry[i] as MN_Zombie;
				if(js!=null && null!=js.gameData)
				{
					if((js.gameData as GD_Zombie).zombieType.toString().slice(0,3)=="211" )
					{
						havaJS	= true;
						jsNum++;
						js.setState(10002000,true);
					}
					
				}
			}
			
			
			if(havaBadWall){
				if(!isInbreak)
				{
					setIsInbreak(true);
					if(!havaJS)
						inbreakHandler();
				}
			}
			else{
				if(havaJS)setIsInbreak(true);
				else
					if(isInbreak)inbreakOver();
			}
			return (havaBadWall || havaJS);
		}
		
		/**
		 *僵尸入侵处理 
		 * @param ob
		 * 
		 */
		public static function inbreakHandler(obAry:Array=null):void
		{
			M_Communication.instance.send(127008);
		}		 
		/**
		 *入侵结束 
		 */
		public static function inbreakOver():void
		{
			M_Communication.instance.send(127009);
		}
		
		//*************************************************** 
		
		//***********************************扩建Wall*****************************
		/**
		 *移出所有类型不为211的在安全区的僵尸 
		 */
		public static function zombieToNoHaven():void
		{
			var grid:GD_MapGrid;
			var layerMapTargetsChildren:Number=GV_Map.instance.layerMapTargets.numChildren;
			var gridAry:Array	= new Array;
			for(var i:uint=0;i<layerMapTargetsChildren;i++)
			{
				var js:MN_Zombie = GV_Map.instance.layerMapTargets.getChildAt(i) as MN_Zombie;
				
				if(null!=js && (js.gameData as GD_Zombie).zombieType.toString().slice(0,3)!="211"  && js.mapGrid.isHaven )
				{
					js.setPosition(GetNoHavenGrid(js.mapGrid) );
				}
				
			}
		}
		
		public static function GetNoHavenGrid(gridV:GD_MapGrid):GD_MapGrid
		{
			if(!gridV.isHaven && !gridV.isWall && !gridV.building)
				return gridV;
			
			var dir:uint=1;
			var grid:GD_MapGrid;
			while(true)
			{
				for(var x:int=-dir; x<=dir; x++)
					for(var y:int=-dir; y<=dir; y++)
					{
						if(x!=-dir && x!=0 && x!=dir && y!=-dir && y!=0 && y!=-dir)
							continue;
						
						var pt:Point	=	new Point(gridV.atx+x, gridV.aty+y);
						if(!GV_Map.instance.checkPoint(pt))continue;
						
						grid = GV_Map.instance.getCoordinateGrid(pt);
						if(!grid.isHaven && !grid.isWall && !grid.building)
							return grid;
						
					}
				
				dir++;
				if(dir>GV_Map.instance.widthNum){throw new GameError("僵尸没地方可站");return null;}
				
			}
			
			return null;
		}
	}
}